package com.kgc.community.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.community.pojo.*;
import com.kgc.community.service.DeviceService;
import com.kgc.community.service.PersonnelService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.lang.Exception;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author dingchangXiao
 * @create 2020-11-29  13:39  com.kgc.community.controller  null.java
 */

@RequestMapping("device")
@Controller
public class deviceController {



    @Resource
    DeviceService deviceService;


    /**
     * 查询
     * @return personnel_list
     */
    @RequestMapping("query.do")
    public String selectAll(HttpServletRequest request, Model model) {
        Integer pageNum=1;
        if (request.getParameter("pageNum")!=null){
            pageNum=Integer.parseInt(request.getParameter("pageNum"));
        }
        String startTime=request.getParameter("startTime")==null?"":request.getParameter("startTime");
        String endTime=request.getParameter("endTime")==null?"":request.getParameter("endTime");
        String selectBy=request.getParameter("selectBy")==null?"":request.getParameter("selectBy");
        Integer pageSize=5;
        PageInfo<Equipment> pageInfo = deviceService.selectAllDevice("%"+selectBy+"%", startTime,endTime,pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("selectBy", selectBy);
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);
        return "device_list";
    }


    /**
     * 删除单条
     * @return personnel_list
     */
    @RequestMapping("delete.do")
    @ResponseBody
    public String deleteDevice(HttpServletRequest request) {
        Integer id = Integer.parseInt(request.getParameter("id"));
        int count = deviceService.deleteDevice(id);
        if (count > 0) {
            return "{\"status\":\"1\"}";
        }
        return "{\"status\":\"2\"}";

    }


    /**
     * 批量删除
     * @return personnel_list
     */
    @RequestMapping("deleteAll.do")
    public String deleteAll(HttpServletRequest request, Model model) {
        String[] subs = request.getParameterValues("sub");
        int sum = 0;
        int count = 0;
        for (String sub : subs) {
            Integer id = Integer.parseInt(sub);
            count = deviceService.deleteDevice(id);
            sum += count;
        }
        if (sum == subs.length & sum > 0) {
            model.addAttribute("deleteAllresult", true);
            return "forward:query.do";
        }
        model.addAttribute("deleteAllresult", false);
        return "forward:query.do";
    }


    /**
     * 进入添加页面
     * 查询小区号
     * @return personnel_add
     */
    @RequestMapping("inAdd.do")
    public String inAdd(Model model){
        List<Village> villages = deviceService.selectVillageName();
        model.addAttribute("villages", villages);
        return "device_add";
    }


    /**
     * 添加
     * @return
     */
    @RequestMapping("add.do")
    public String add(HttpServletRequest request, Model model){
        Map<String, String[]> map = request.getParameterMap();
        Equipment equipment = new Equipment();
        try {
            BeanUtils.populate(equipment, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer villageId = Integer.parseInt(request.getParameter("villageId"));
        equipment.setVillageId(villageId);
        int count = deviceService.addDevice(equipment);
        if (count > 0) {
            model.addAttribute("addResult", true);
            return "forward:query.do";
        }
        model.addAttribute("addResult", false);
        return "forward:query.do";
    }


    /**
     * 修改
     * @return personnel_modify
     */
    @RequestMapping("selectDevice.do")
    public String select(HttpServletRequest request, Model model) {
        /*查询小区号*/
        List<Village> villages = deviceService.selectVillageName();
        model.addAttribute("villages", villages);
        /*查询其他信息*/
        Integer equipmentId = Integer.parseInt(request.getParameter("id"));
        Equipment equipment = deviceService.selectDevice(equipmentId);
        model.addAttribute("equipment", equipment);
        return "device_modify";
    }

    @RequestMapping("modify.do")
    public String modify(HttpServletRequest request, Model model){
        Map<String, String[]> map = request.getParameterMap();
        Equipment equipment = new Equipment();
        try {
            BeanUtils.populate(equipment, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer villageId = Integer.parseInt(request.getParameter("villageId"));
        equipment.setVillageId(villageId);
        int count = deviceService.modifyDevice(equipment);
        if (count > 0) {
            model.addAttribute("modifyResult", true);
            return "forward:query.do";
        }
        model.addAttribute("modifyResult", false);
        return "forward:query.do";
    }

}
